package com.richapp.launcher.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.key.virtual.ir.InputKeyValueJni;
import com.richapp.launcher.control.ControlProcess;
import com.richapp.launcher.gpio.GPIOServer;
import com.richapp.launcher.ui.activities.ScreenSaverAdvActivity;
import com.richapp.launcher.ui.activities.ScreenOnAdvActivity;


/**
 * Created by jack on 2015/5/11 0011.
 */
public class LauncherMessageReceiver extends BroadcastReceiver {

    public static final String ACTION_USER_UPDATE_INFO = "com.richapp.launcher.user.update_info";

    public static final String ACTION_PHONE_ADD = "com.richapp.launcher.phone.add";
    public static final String ACTION_PHONE_DEL = "com.richapp.launcher.phone.del";
    public static final String ACTION_PHONE_REFUSE = "com.richapp.launcher.phone.refuse";

    public static final String ACTION_SENSOR_STATUS = "com.richapp.launcher.sensor.status";
    public static final String ACTION_SENSOR_ALARM = "com.richapp.launcher.sensor.alarm";
    public static final String ACTION_SCREEN_SAVER_START = "com.richapp.launcher.screen.saver.start";


    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
//        if(ACTION_PHONE_REFUSE.equals(action)){
//            LoggerManager.getInstance().getiLogger().info(getClass(),"收到控制手机指令!");
//
//            String refuseFlag = intent.getStringExtra("refuseFlag");
//            String phoneUid = intent.getStringExtra("phoneUid");
//
//            if(phoneUid == null || "".equals(phoneUid.trim()) || (!"true".equals(refuseFlag) && !"false".equals(refuseFlag)) ){
//                LoggerManager.getInstance().getiLogger().error(getClass(), "launcher参数错误!");
//            }else{
//                BoxModel currentBox = new BoxModelDao(context).getCurrentBox();
//                BoxPhone boxPhone = new BoxPhoneDao(context).getWithPhoneUid(currentBox.getBoxCpuId(),phoneUid);
//                if(boxPhone != null){
//                    boxPhone.setRefuseFlag(refuseFlag);
//                    new BoxPhoneDao(context).add(boxPhone);
//                    if("true".equals(boxPhone.getRefuseFlag())){
//
//                        UserLogin userLogin = new UserLoginDao(context).getUserLoginByPhoneUid(phoneUid);
//                        if(userLogin != null){
//                            UserLogoutReply ulr = new UserLogoutReply();
//                            ulr.setCurrentBoxCpuId(currentBox.getBoxCpuId());
//                            ulr.setMsg("该终端被拒绝连接!");
//                            String reply = JSON.toJSONString(ulr);
//
//                            UserServer.getInstance().refuseClient(userLogin.getClientId(),reply);
//                        }
//                    }
//                }else{
//                    LoggerManager.getInstance().getiLogger().error(getClass(),"对应的boxPhone不存在!");
//                }
//            }
//
//            BoxPhone boxPhone = new BoxPhoneDao(context).getWithPhoneUid(new BoxModelDao(context).getCurrentBox().getBoxCpuId(),phoneUid);
//           msg;
//            if(Boolean.parseBoolean(refuseFlag)){
//                msg = "拒绝手机:"+phoneName+"连接";
//            }else{
//                msg = "允许手机:"+phoneName+"连接";
//            }
//
//            Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
//            toast.setGravity(Gravity.CENTE String phoneName = "";
//            if(boxPhone != null){
//                if(!StringUtil.isNullOrEmpty(boxPhone.getPhoneName())){
//                    phoneName = boxPhone.getPhoneName();
//                }
//            }
//
//            String R, 0, 0);
//            toast.show();
//        }else

        if (ACTION_SCREEN_SAVER_START.equals(action)) {
            IntentFilter recevierFilter = new IntentFilter();
            recevierFilter.addAction(Intent.ACTION_SCREEN_ON);
            context.getApplicationContext().registerReceiver(this, recevierFilter);
            ControlProcess.getInstance();

            //showAdvPicscircularly(context);
        } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
            Log.e(LauncherMessageReceiver.class.getName(), "action screen on");
            context.getApplicationContext().unregisterReceiver(this);

            //showScreenOnAdv(context);
        }
    }

    private void showScreenOnAdv(Context context) {
        Intent intent = new Intent(context, ScreenOnAdvActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    private void showAdvPicscircularly(Context context) {
        Intent intent = new Intent(context, ScreenSaverAdvActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);

    }

}
